using System;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 抽象SimulateBehaviour
/// </summary>
public partial class AbsHFSimulateBehaviour:IHFObject
{
    /// <summary>
    /// 模拟Behaviour事件组
    /// enHFSimulateBehaviourEvent组
    /// </summary>
    protected virtual int[] simulateBehaviourEvents { get { return null; } }

    #region OnBindSimulateBehaviourEvent 绑定模拟事件
    /// <summary>
    /// 当前已绑定的事件组
    /// </summary>
    List<int> mBindEvents = new List<int>();
    /// <summary>
    /// 绑定模拟事件
    /// </summary>
    void OnBindSimulateBehaviourEvent()
    {
        #region 收集要绑定的事件
        if (hasRectTransform)
        {
            foreach (int key in HFSimulateBehaviour_BindEventComponentMap.msrDefaultSimulateUIBehaviourEvents)
            {
                if (!mBindEvents.Contains(key))
                {
                    mBindEvents.Add(key);
                }
            }
        }
        else
        {
            foreach (int key in HFSimulateBehaviour_BindEventComponentMap.msrDefaultSimulateMonoBehaviourEvents)
            {
                if (!mBindEvents.Contains(key))
                {
                    mBindEvents.Add(key);
                }
            }
        }
        if (simulateBehaviourEvents != null && simulateBehaviourEvents.Length > 0)
        {
            foreach (var key in simulateBehaviourEvents)
            {
                if (!mBindEvents.Contains(key))
                {
                    mBindEvents.Add(key);
                }
            }
        }
        #endregion

        #region 收集GameObject已模拟的事件
        int goId = gameObject.GetInstanceID();
        BindSimulateBehaviour_Collect bsbc = null;
        if (mBindSimulateBehaviour_Collect.TryGetValue(goId, out bsbc))
        {
            if (bsbc == null)
            {
                bsbc = gameObject.GetComponent<BindSimulateBehaviour_Collect>();
                if (bsbc == null)
                {
                    bsbc = gameObject.AddComponent<BindSimulateBehaviour_Collect>();
                }
                mBindSimulateBehaviour_Collect[goId] = bsbc;
            }
        }
        else
        {
            bsbc = gameObject.GetComponent<BindSimulateBehaviour_Collect>();
            if (bsbc == null)
            {
                bsbc = gameObject.AddComponent<BindSimulateBehaviour_Collect>();
            }
            mBindSimulateBehaviour_Collect.Add(goId, bsbc);
        }
        #endregion

        foreach (var key in mBindEvents)
        {
            OnBindEvent(key, bsbc);
        }
    }

    /// <summary>
    /// 模拟绑定组件收集器
    /// Key:GameObject的GetInstanceID
    /// Value:收集器
    /// </summary>
    Dictionary<int, BindSimulateBehaviour_Collect> mBindSimulateBehaviour_Collect = new Dictionary<int, BindSimulateBehaviour_Collect>();
    /// <summary>
    /// 绑定指定事件到GameObject
    /// </summary>
    /// <param name="_bindEvtKey">绑定事件Key</param>
    /// <param name="_bsbc">绑定模拟组件收集器</param>
    void OnBindEvent(int _bindEvtKey, BindSimulateBehaviour_Collect _bsbc)
    {
        Type type = null;
        Dictionary<int, Type> map = HFSimulateBehaviour_BindEventComponentMap.msrSimulateMonoBehaviourBindEventKeyMap;
        IBindSimulateBehaviour bsb = _bsbc.GetBindSimulateBehaviour(_bindEvtKey);
        if (bsb != null)
        {
            bsb.BindSimulateBehaviour(this);
        }
        else
        {
            if (map.TryGetValue(_bindEvtKey, out type))
            {
                _bsbc.BindSimulateBehaviour(_bindEvtKey, type, this);                
            }
            else
            {
                HFP.HFError($"Can't find  bindEvtKey for {_bindEvtKey} in enHFSimulateBehaviourEvent");
            }
        }
    }
    #endregion

    #region OnUnBindSimulateBehaviourEvent 取消绑定模拟事件
    /// <summary>
    /// 取消绑定模拟事件
    /// </summary>
    void OnUnBindSimulateBehaviourEvent()
    {
        BindSimulateBehaviour_Collect bsbc = null;
        if (mBindSimulateBehaviour_Collect.TryGetValue(bindGameObjectInstanceId, out bsbc))
        {
            bsbc.RemoveBindEventBehaviour(this);
        }
        mBindSimulateBehaviour_Collect.Remove(bindGameObjectInstanceId);
    }
    #endregion
}